<template>
  <view class="index">
    <view class="jtop">
      <view class="status_bar"><!-- 这里是状态栏 --></view>
      <view class="jtop_l" @tap="navigateBack"
        ><image src="../../static/img/ic_back.png"></image
      ></view>
      <view class="jtop_c">性别</view>
    </view>
    <view class="tophr"></view>

    <view class="uebs">
      <!-- 	<view class="ueb" @click="btsex">
				<text>男</text>
				<image src="../../static/img/user/ic_check.png" v-if="sex=0"></image>
			</view>
			<view class="ueb" @click="btsex">
				<text>女</text>
				<image src="../../static/img/user/ic_check.png" v-if="sex=1"></image>
			</view>
			<view class="ueb" @click="btsex">
				<text>保密</text>
				<image src="../../static/img/user/ic_check.png" v-if="sex=2"></image>
			</view> -->

      <view
        class="ueb"
        :class="current == item.value ? 'selectBox' : ''"
        @click="labelBtn(item.value)"
        v-for="(item, index) in items"
        :key="item.value"
      >
        <text>{{ item.name }}</text>
        <image src="../../static/img/user/ic_check.png"></image>
      </view>
    </view>
  </view>
</template>

<script>
import request from '@/common/request.js';
import { mapState, mapMutations } from 'vuex';

export default {
  data() {
    return {
      repeat_click: true,
      items: [
        {
          value: 1,
          name: '女',
        },
        {
          value: 2,
          name: '男',
        },
        // {
        //     value: 2,
        //     name: '保密'
        // }
      ],
      current: 0,
    };
  },
  computed: {
    ...mapState(['hasLogin', 'forcedLogin']),
  },
  onLoad() {
    this.current = uni.getStorageSync('user').userInfoResponse.gender;
  },
  methods: {
    labelBtn(index) {
      if (this.current == index) {
        return;
      }
      this.current = index;
      console.log(index);
      for (let i in this.items) {
        this.items[i].checked = false;
      }

      console.log(this.current);

      var a = '';
      if (this.current == 1) {
        // a="FAMALE"
        a = '1';
      }
      if (this.current == 2) {
        // a="MALE"
        a = '2';
      }
      if (this.current == 22) {
        a = 'OTHER';
      }
      request({
        url: '/ucenter/user/modify',
        data: {
          gender: a,
        },

        method: 'PUT',
        success: openIdRes => {
          console.log(openIdRes);

          // 已经授权了、查询到用户的数据了
          if (openIdRes.data.success == true) {
            var aa = uni.getStorageSync('user');
            aa.userInfoResponse['gender'] = this.current;
            uni.setStorageSync('user', aa);

            this.repeat_click = true;
            uni.showToast({
              title: '修改成功',
            });
          } else {
            // 错误
            this.repeat_click = true;
            uni.showToast({
              title: openIdRes.data.msg,
              icon: 'none',
            });
            this.isLoading = false;
          }
        },
        fail: () => {
          this.repeat_click = true;
          uni.showToast({
            title: '服务器开小差，请稍后~',
            icon: 'none',
          });
          return false;
        },
      });
    },

    navigateBack() {
      uni.navigateBack();
    },
  },
};
</script>

<style>
.uebs {
  width: 100%;
  height: auto;
  display: block;
  float: left;
  margin: 20rpx 0 0 0;
  border: 1px solid #dddddd;
  border-left: 0;
  border-right: 0;
  background: #ffffff;
}

.ueb {
  width: 720rpx;
  height: auto;
  display: block;
  float: left;
  border-bottom: 1px solid #dddddd;
  margin: 0 0 0 30rpx;
  background: #ffffff;
}
.ueb text {
  width: auto;
  height: 42rpx;
  display: block;
  float: left;
  font-size: 32rpx;
  line-height: 42rpx;
  text-align: right;
  color: #000000;
  margin: 24rpx 0rpx 24rpx 30rpx;
}
.ueb input {
  width: 530rpx;
  height: 42rpx;
  display: block;
  float: right;
  margin: 20rpx 30rpx 0 0;
  font-size: 34rpx;
  line-height: 42rpx;
  color: #000000;
}
.ueb image {
  width: 48rpx;
  height: 48rpx;
  display: none;
  float: right;
  margin: 20rpx 30rpx 0 0;
}

.ueb:last-child {
  border: 0;
}

.index {
  background: rgb(242, 242, 242);
  width: 100%;
  height: auto;
}
.jtop {
  width: 100%;
  height: 88rpx;
  background: #ffffff;
  position: fixed;
  top: 0;
  left: 0;
  z-index: 1000;
  box-shadow: inset 0px -1px 0px #dddddd;
}
.jtop_l {
  width: auto;
  height: 88rpx;
  display: block;
  float: left;
  font-size: 32rpx;
  color: #000000;
  line-height: 88rpx;
  margin: 0 0 0 30rpx;
}
.jtop_l image {
  width: 48rpx;
  height: 48rpx;
  display: block;
  float: left;
  margin: 20rpx 0 20rpx 0;
}
.jtop_lt {
  margin: 0 0 0 10rpx;
}
.jtop_r {
  width: 48rpx;
  height: 48rpx;
  display: block;
  float: right;
  margin: 20rpx 20rpx 0 0;
}
.jtop_rl {
  margin: 20rpx 30rpx 0 0;
}
.jtop_r image {
  width: 48rpx;
  height: 48rpx;
  display: block;
  float: left;
}
.jtop_c {
  width: 70%;
  height: 48rpx;
  line-height: 48rpx;
  display: block;
  float: left;
  position: fixed;
  top: 20rpx;
  left: 15%;
  text-align: center;
  font-size: 36rpx;
  color: #000000;
}
.jtop_c image {
  width: 36rpx;
  height: 36rpx;
  display: inline-block;
  float: none;
  margin-bottom: -5rpx;
  margin-left: 5rpx;
}
.jtop_b {
  width: 100%;
  height: auto;
  background: #ffffff;
  position: fixed;
  top: 88rpx;
  left: 0;
  z-index: 1001;
}
.tophr {
  width: 100%;
  height: 88rpx;
}

.ueb.selectBox image {
  display: block;
}
</style>
